package com.live.shop.scene.server.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.live.shop.R;
import com.live.shop.model.ProductMaterialRecordModel;

import java.io.File;
import java.util.List;

public class MaterialAdapter extends RecyclerView.Adapter<MaterialAdapter.AdapterHolder> {
    private final Context mContext;
    //素材列表
    private final List<ProductMaterialRecordModel> list;
    //选中的post
    private int selectPosition = -1;
    //Glide加载管理器
    private final RequestManager mRequestManager;
    //素材选选择监听器
    private OnMaterialClickListener materialListener;

    public MaterialAdapter(Context context, List<ProductMaterialRecordModel> mlist, RequestManager mRequestManager) {
        this.mContext = context;
        this.list = mlist;
        this.mRequestManager = mRequestManager;
    }

    @NonNull
    @Override
    public AdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AdapterHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ws_main_material, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterHolder holder, @SuppressLint("RecyclerView") int position) {
        //根据当前类似是否为视频进行显示
        ProductMaterialRecordModel model = list.get(position);

        holder.item_ws_main_material_name.setText(model.getMaterialName());
        //是否选中
        if(selectPosition == position){
            holder.item_ws_main_material_name.setTextColor(mContext.getColor(R.color.white));
            holder.item_ws_main_linearlayout.setBackgroundResource(R.drawable.bg_stroke_10dip_blue);
        }else{
            holder.item_ws_main_material_name.setTextColor(mContext.getColor(R.color.gray));
            holder.item_ws_main_linearlayout.setBackgroundResource(R.drawable.bg_stroke_10dip_gray);
        }
        //隐藏状态 0 显示 1隐藏
        if(model.getHideState() == 0){
            holder.item_ws_main_material_hidestate.setBackgroundResource(R.mipmap.icon_ws_main_show);
        }else{
            holder.item_ws_main_material_hidestate.setBackgroundResource(R.mipmap.icon_ws_main_hide);
        }

        //素材类型 0.图片 1.视频 2.文字
        if(model.getMaterialType() != 1){
            holder.item_ws_main_material_video_player.setVisibility(View.GONE);
            mRequestManager.load(new File(model.getMaterialFilepath()))
                    .placeholder(R.mipmap.icon_image_rectangle_default)
                    .into(new CustomTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            holder.item_ws_main_material_image.setBackground(resource);
                        }
                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {

                        }
                    });
        }else{
            //视频
            holder.item_ws_main_material_video_player.setVisibility(View.VISIBLE);
            try {
                //引用形式的描述信息：获取视频文件的预览图
                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(model.getMaterialFilepath());
                mRequestManager.load(retriever.getFrameAtTime())
                        .placeholder(R.mipmap.icon_image_rectangle_default)
                        .into(new CustomTarget<Drawable>() {
                            @Override
                            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                holder.item_ws_main_material_image.setBackground(resource);
                            }
                            @Override
                            public void onLoadCleared(@Nullable Drawable placeholder) {

                            }
                        });
                retriever.release();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置选中的位置
                selectPosition = position;
                if(materialListener != null){
                    materialListener.onMaterialClick(position);
                }
                notifyDataSetChanged();
            }
        });
        holder.itemView.setTag(this);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    static class AdapterHolder extends RecyclerView.ViewHolder {
        LinearLayout item_ws_main_linearlayout;
        ImageView item_ws_main_material_image;
        ImageView item_ws_main_material_video_player;
        ImageView item_ws_main_material_hidestate;
        TextView item_ws_main_material_name;
        public AdapterHolder( View itemView) {
            super(itemView);
            item_ws_main_linearlayout = itemView.findViewById(R.id.item_ws_main_linearlayout);
            item_ws_main_material_image = itemView.findViewById(R.id.item_ws_main_material_image);
            item_ws_main_material_video_player = itemView.findViewById(R.id.item_ws_main_material_video_player);
            item_ws_main_material_hidestate = itemView.findViewById(R.id.item_ws_main_material_hidestate);
            item_ws_main_material_name = itemView.findViewById(R.id.item_ws_main_material_name);
        }
    }

    //切换素材
    public void setLastOrNextMaterial(int position){
        selectPosition = position;
        notifyDataSetChanged();
    }
    //素材显隐
    public void setMaterialHideState(int position,int hideState){
        selectPosition = position;
        list.get(selectPosition).setHideState(hideState);
        notifyDataSetChanged();
    }
    //素材点击
    public void setOnMaterialClickListener(OnMaterialClickListener onMaterialListener) {
        materialListener = onMaterialListener;
    }
}
